package com.smile.service.impl;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import com.smile.model.dto.request.DeleteFileRequestDTO;
import com.smile.model.response.response.PutFileResponseDTO;
import com.smile.service.OssService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
public class OssServiceImpl implements OssService {

    @Value("${oss.endpoint}")
    public String endpoint;

    @Value("${oss.bucketName}")
    public String bucketName;

    @Value("${oss.region}")
    public String region;

    @Value("${oss.accessKeyId}")
    public String accessKeyId;

    @Value("${oss.accessKeySecret}")
    public String accessKeySecret;

    private OSS ossClient;

    @PostConstruct
    public void init() throws ClientException {

        //String accessKeyId, String secretAccessKey

        CredentialsProvider credentialsProvider = new com.aliyun.oss.common.auth.DefaultCredentialProvider(accessKeyId, accessKeySecret);


        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();
    }

    @Override
    public PutFileResponseDTO putFile(String path, String fileName, MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();


        String objectName = fileName;

        objectName = objectName.replaceAll("\\\\", "/");

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes));

        // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
//         ObjectMetadata metadata = new ObjectMetadata();
//         metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
//         metadata.setObjectAcl(CannedAccessControlList.Private);
//         putObjectRequest.setMetadata(metadata);

        // 上传字符串。
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        System.out.println(result.getETag());
        System.out.println(result.getResponse());
        PutFileResponseDTO putFileResponseDTO = new PutFileResponseDTO();
        putFileResponseDTO.setUrl(("http://www.smile-emo.com" + "/" + objectName).replaceAll("\\\\", "/"));
        putFileResponseDTO.setETag(result.getETag());
        return putFileResponseDTO;
    }

    @Override
    public PutFileResponseDTO putFile(String path, String fileName, InputStream file) throws IOException {
        byte[] bytes = file.readAllBytes();


        String objectName = fileName;

        objectName = objectName.replaceAll("\\\\", "/");

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes));

        // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
//         ObjectMetadata metadata = new ObjectMetadata();
//         metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
//         metadata.setObjectAcl(CannedAccessControlList.Private);
//         putObjectRequest.setMetadata(metadata);

        // 上传字符串。
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        System.out.println(result.getETag());
        System.out.println(result.getResponse());
        PutFileResponseDTO putFileResponseDTO = new PutFileResponseDTO();
        putFileResponseDTO.setUrl(("http://www.smile-emo.com" + "/" + objectName).replaceAll("\\\\", "/"));
        putFileResponseDTO.setETag(result.getETag());
        return putFileResponseDTO;
    }

    @Override
    public Boolean deleteFile(DeleteFileRequestDTO deleteFileRequestDTO) {
        ossClient.deleteObject(bucketName, deleteFileRequestDTO.getPath() + "/" + deleteFileRequestDTO.getFileName());
        return Boolean.FALSE;
    }
}
